<template>
    <div class="bx-editor">
        <van-nav-bar title="编辑信息" left-text="返回" left-arrow border fixed @click-left="$router.go(-1)" />
        <div style="height:50px"></div>
        <div class="title">
            基本信息
        </div>
        <van-cell-group>
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>教育方向选择</div>
                <van-radio-group v-model="edu_direction">
                    <van-radio :name="1" checked-color="#366cb3">学历</van-radio>
                    <van-radio :name="2" checked-color="#366cb3">技能</van-radio>
                </van-radio-group>
            </div>
            <van-field readonly clickable required label="产品选择" :value="goodsName " placeholder="--请选择--" @click="goodsPicker = true" />
            <van-field v-model="name" label="姓名" placeholder="请输入姓名" size="large" required />
            <van-field v-model="mobile" label="联系电话" placeholder="请输入联系电话" size="large" required />
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>性别</div>
                <van-radio-group v-model="sex">
                    <van-radio :name="'男'" checked-color="#366cb3">男</van-radio>
                    <van-radio :name="'女'" checked-color="#366cb3">女</van-radio>
                </van-radio-group>
            </div>
            <van-field readonly clickable required label="出生日期" :value="$formatDateTime(birthday, 'yyyy-MM-dd')" placeholder="--请选择--" @click="timePopupShow1 = true" />
            <van-popup v-model="timePopupShow1" position="bottom">
                <van-datetime-picker :min-date="minDate" :max-date="maxDate" v-model="timeVal1" type="date" @cancel="timePopupShow1 = false" @confirm="timePopupShow1 = false" />
            </van-popup>

            <van-field v-model="id_card" label="身份证号" placeholder="请输入身份证号" size="large" required />
            <van-field v-model="price" label="学费" placeholder="请输入学费" size="large" required />
            <van-field v-model="address" label="家庭地址" placeholder="请输入家庭地址" size="large" required />
            <van-field readonly clickable required label="报名时间" :value="timeTurn(edu_enter_time)" placeholder="--请选择--" @click="timePop1 = true" right-icon="clock-o" />
            <van-field readonly clickable required label="开始学习时间" :value="timeTurn(start_time)" placeholder="--请选择--" @click="timePop2 = true" right-icon="clock-o" />
            <van-field readonly clickable required label="结束学习时间" :value="timeTurn(over_time)" placeholder="--请选择--" @click="timePop3 = true" right-icon="clock-o" />
            <van-field readonly clickable required label="毕业/领证时间" :value="timeTurn(end_time)" placeholder="--请选择--" @click="timePop4 = true" right-icon="clock-o" />
            <van-field readonly clickable required label="学习标准" :value="edu_standard" placeholder="--请选择--" @click="learnNormPicker = true" />
            <van-field readonly clickable required label="报考级别" :value="edu_level" placeholder="--请选择--" @click="examRankPicker = true" />
            <van-field label="前置学历" v-model="edu_education" placeholder="请输入前置学历" size="large" required />
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>学位申请</div>
                <van-radio-group v-model="edu_degree">
                    <van-radio :name="1" checked-color="#366cb3">是</van-radio>
                    <van-radio :name="0" checked-color="#366cb3">否</van-radio>
                </van-radio-group>
            </div>
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>是否通过</div>
                <van-radio-group v-model="edu_pass">
                    <van-radio :name="1" checked-color="#366cb3">是</van-radio>
                    <van-radio :name="0" checked-color="#366cb3">否</van-radio>
                </van-radio-group>
            </div>
        </van-cell-group>

        <van-popup v-model="goodsPicker" position="bottom">
            <van-picker show-toolbar value-key="name" :columns="goodsList" @cancel="goodsPicker = false" @confirm="onGoodsSelect" />
        </van-popup>
        <van-popup v-model="learnNormPicker" position="bottom">
            <van-picker show-toolbar :columns="learnNormList" @cancel="learnNormPicker = false" @confirm="onLearnNorm" />
        </van-popup>
        <van-popup v-model="examRankPicker" position="bottom">
            <van-picker show-toolbar :columns="examRankList" @cancel="examRankPicker = false" @confirm="onExamRank" />
        </van-popup>
        <!-- 报名时间 -->
        <van-popup v-model="timePop1" position="bottom">
            <van-datetime-picker v-model="timeVal1" type="date" :min-date="minDate" @cancel="timePop1 = false" @confirm="timeConfirm(1)" />
        </van-popup>
        <!-- 开始学习时间 -->
        <van-popup v-model="timePop2" position="bottom">
            <van-datetime-picker v-model="timeVal2" type="date" :min-date="minDate" @cancel="timePop2 = false" @confirm="timeConfirm(2)" />
        </van-popup>
        <!-- 结束学习时间 -->
        <van-popup v-model="timePop3" position="bottom">
            <van-datetime-picker v-model="timeVal3" type="date" :min-date="minDate" @cancel="timePop3 = false" @confirm="timeConfirm(3)" />
        </van-popup>
        <!-- 毕业/领证时间 -->
        <van-popup v-model="timePop4" position="bottom">
            <van-datetime-picker v-model="timeVal4" type="date" :min-date="minDate" @cancel="timePop4 = false" @confirm="timeConfirm(4)" />
        </van-popup>
        <div class="title">
            发票信息
        </div>
        <van-cell-group>
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>是否允许后付款</div>
                <van-radio-group v-model="lag_pay">
                    <van-radio :name="1" checked-color="#366cb3">是</van-radio>
                    <van-radio :name="0" checked-color="#366cb3">否</van-radio>
                </van-radio-group>
            </div>
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>付款方式</div>
                <van-radio-group v-model="payment_type">
                    <van-radio v-for="(item, index) in payTypeSource" :key="index" :name="item.id" checked-color="#366cb3">{{item.name}}</van-radio>
                </van-radio-group>
            </div>

            <van-field readonly clickable required label="发票类型" :value="invoiceTypeName" placeholder="--请选择--" @click="invoicePickerShow = true" />
            <van-popup v-model="invoicePickerShow" position="bottom">
                <van-picker show-toolbar :columns="invoiceTypeSource" :default-index="invoiceTypeIndex" value-key="name" @cancel="invoicePickerShow = false" @confirm="onInvoice" />
            </van-popup>

            <van-field readonly clickable required label="开票类型" :value="invoiceClassName" placeholder="--请选择--" @click="invoiceClassPickerShow = true" />
            <van-popup v-model="invoiceClassPickerShow" position="bottom ">
                <van-picker show-toolbar :columns="invoiceClassSource" :default-index="invoiceClassIndex" value-key="name" @cancel="invoiceClassPickerShow = false" @confirm="onMakeInvoice" />
            </van-popup>

            <van-field readonly clickable required label="发票名称" :value="invoiceNameName" placeholder="--请选择--" @click="invoiceNameShow = true" />
            <van-popup v-model="invoiceNameShow" position="bottom">
                <van-picker show-toolbar :columns="invoiceNameSource" :default-index="invoiceNameIndex" value-key="name" @cancel="invoiceNameShow = false" @confirm="onInvoiceName" />
            </van-popup>
            <van-field v-if="invoiceNameName == '自定义'" v-model="new_invoice_name" label="自定义发票名称" placeholder="请输入发票名称" size="large" required />
            <van-field v-model="invoice_company" label="公司名称" placeholder="请输入公司名称" size="large" required />
            <van-field v-model="invoice_tax_code" label="税号" placeholder="请输入税号" size="large" required />
            <van-field v-model="invoice_address" label="公司地址" placeholder="请输入公司地址" size="large" required />
            <van-field v-model="invoice_mobile" label="公司联系电话" placeholder="请输入公司联系电话" size="large" required />
            <van-field v-model="invoice_bank" label="开户行" placeholder="请输入开户行" size="large" required />
            <van-field v-model="invoice_bank_code" label="开户账号" placeholder="请输入开户账号" size="large" required />
            <div class="radio">
                <div><span style="color: #ee0a24">*</span>是否见票付款</div>
                <van-radio-group v-model="invoice_pay">
                    <van-radio :name="1" checked-color="#366cb3">是</van-radio>
                    <van-radio :name="0" checked-color="#366cb3">否</van-radio>
                </van-radio-group>
            </div>
        </van-cell-group>
        <div class="foot-btn">
            <div class="blue-btn" @click="pushInfo()">保存信息</div>
        </div>
    </div>
</template>

<script>
export default {
    name: "educationSet",
    data() {
        return {
            edu_direction: 1,// 教育方向选择

            goodsIndex: '',//  产品index
            goodsName: '',//  产品名称
            price: '',//  学费

            timePop1: false,
            timeVal1: new Date(),
            edu_enter_time: new Date(),//  报名时间

            timePop2: false,
            timeVal2: new Date(),
            start_time: new Date(),//  学习时间	
            
            timePop3: false,
            timeVal3: new Date(),
            over_time: new Date(),//  学习时间	

            timePop4: false,
            timeVal4: new Date(),
            end_time: new Date(),//  毕业/领证时间		

            edu_standard: '',//  学习标准	
            edu_level: '',// 报考级别

            edu_education: '',// 前置学历

            edu_degree: 1,// 学位申请
            edu_pass: 1,// 学位申请


            goodsPicker: false,
            goodsValue: '',
            goodsList: [],

            learnNormPicker: false,
            learnNormValue: '',
            learnNormList: [],

            examRankPicker: false,
            examRankValue: '',
            examRankList: [],


            minDate: new Date(1920, 0, 1),
            maxDate: new Date(2030, 0, 1),

            salesmanName: '',
            salesmanValArr: [],
            salesmanSource: [],


            invoiceTypeName: '',
            invoiceTypeIndex: 0,
            invoicePickerShow: false,
            invoiceTypeSource: [],

            invoiceClassName: '',
            invoiceClassIndex: 0,
            invoiceClassPickerShow: false,
            invoiceClassSource: [],

            invoiceNameName: '',
            invoiceNameIndex: 0,
            invoiceNameShow: false,
            invoiceNameSource: [], //发票名称

            payTypeSource: [], // 付款方式
            moneyRadio: '1',
            isVoucherRadio: '1',
            paymentRadio: '1',
            mask: '',

            addressPopupShow: false,
            site: '',


            name: '', //合作客户名称
            sex: '男', //性别
            id_card: '', //身份证号
            address: '', //家庭地址

            timeField1: false,
            timeVal1: new Date(),
            birthday: new Date(),// 出生日期
            timePopupShow1: false,
            mobile: '', //联系方式	
            industrySource: [
                {
                    name: ''
                }
            ],

            channelSource: [
                {
                    name: ''
                }
            ], //客户类型


            education: '', //前置学历
            salesman_id: '', //共享业务员（非必填）
            is_brokerage: 0, //是否产生佣金
            brokerage: '', //佣金
            remark: '', //特殊要求备注
            cooperation: '', //可延伸合作机会
            lag_pay: 0, //是否允许后付款
            payment_type: 1, //付款方式
            need_invoice: '', //是否需要发票，1需要，0不需要
            invoice_type: '', //发票类型，1电子发票，2纸质发票
            invoice_class: '', //开票类型
            invoice_name: '', //发票名称
            new_invoice_name: '', //自定义发票名称
            invoice_company: '', //公司名称
            invoice_tax_code: '', //税号
            invoice_address: '', //公司地址
            invoice_mobile: '', //公司电话	
            invoice_bank: '', //开户行
            invoice_bank_code: '', //开户账号
            invoice_pay: 0, //是否见票付款，1是，0否

            salesnameSource: [],
            value: '',
            students: [],
            showSelect: false,
            businessMan: [],



            basic_information: {},
            name: '',
            sex: '男',
        };
    },
    methods: {
        getConfig() {
            let params = {
                wx_member_id: localStorage.getItem('wx_member_id'),
                token: localStorage.getItem('token'),
                cate_id: 2,
            };
            this.$post("/api/member/get_config_info", params)
                .then(res => {
                    if (res.code == 1) {
                        this.invoiceTypeSource = res.data.invoice_type // 发票类型
                        this.invoiceClassSource = res.data.invoice_class // 开票类型
                        this.invoiceNameSource = res.data.invoice_name // 发票名称	
                        this.industrySource = res.data.industry //  所属行业
                        this.payTypeSource = res.data.payment_type //  付款方式
                        this.channelSource = res.data.channel //  付款方式


                        // 筛选 业务专员选中的选项
                        this.salesmanSource = res.data.salesname // 业务专员
                        let salesmanValArr = []
                        let salesmanNameArr = []
                        res.data.salesname.map((item, index) => {
                            if (item.selected) {
                                salesmanValArr.push(item.value)
                                salesmanNameArr.push(item.name)
                            }
                        })
                        this.salesmanValArr = salesmanValArr
                        this.salesmanName = salesmanNameArr.toString(',')


                        this.getInfo();
                    }
                })
                .catch(err => { });
        },
        getInfo() {
            let params = {
                wx_member_id: localStorage.getItem('wx_member_id'),
                token: localStorage.getItem('token'),
                id: this.$route.query.id,
            };
            this.$post("/api/Education/detail", params)
                .then(res => {
                    if (res.code == 1) {
                        // this.goodsList = res.data.goods
                        // this.learnNormList = res.data.edu_standard_list
                        // this.examRankList = res.data.edu_level_list
                        this.basic_information = res.data.basic_information
                        this.edu_direction = res.data.basic_information.edu_direction
                        this.price = res.data.basic_information.price

                        this.name = res.data.basic_information.name
                        this.sex = res.data.basic_information.sex
                        this.birthday = new Date(res.data.basic_information.birthday)
                        this.id_card = res.data.basic_information.id_card
                        this.address = res.data.basic_information.address
                        this.mobile = res.data.basic_information.mobile



                        this.edu_enter_time = new Date(res.data.basic_information.edu_enter_time_text)
                        this.timeVal1 = new Date(res.data.basic_information.edu_enter_time_text)

                        this.start_time = new Date(res.data.basic_information.start_time_text)
                        this.timeVal2 = new Date(res.data.basic_information.start_time_text)

                        this.over_time = new Date(res.data.basic_information.over_time_text)
                        this.timeVal3 = new Date(res.data.basic_information.over_time_text)

                        this.end_time = new Date(res.data.basic_information.end_time_text)
                        this.timeVal4 = new Date(res.data.basic_information.end_time_text)



                        this.edu_standard = res.data.basic_information.edu_standard
                        this.edu_level = res.data.basic_information.edu_level
                        this.edu_education = res.data.basic_information.edu_education
                        this.edu_degree = res.data.basic_information.edu_degree
                        this.edu_pass = res.data.basic_information.edu_pass



                        // 发票类型
                        this.goodsList.map((item, index) => {
                            if (item.id == res.data.basic_information.goods_id) {
                                this.goodsIndex = index
                                this.goodsName = item.name
                            }
                        })

                        // 发票类型
                        this.invoiceTypeSource.map((item, index) => {
                            if (item.id == res.data.invoice_information.invoice_type) {
                                this.invoiceTypeIndex = index
                                this.invoiceTypeName = item.name
                            }
                        })
                        // 开票类型
                        this.invoiceClassSource.map((item, index) => {
                            if (item.id == res.data.invoice_information.invoice_class) {
                                this.invoiceClassIndex = index
                                this.invoiceClassName = item.name

                            }
                        })

                        // 发票名称
                        this.invoiceNameSource.map((item, index) => {
                            if (item == res.data.invoice_information.invoice_name) {
                                this.invoiceNameIndex = index
                                this.invoiceNameName = item
                                // this.new_invoice_name = item

                            }
                        })

                        this.lag_pay = res.data.invoice_information.lag_pay //是否允许后付款
                        this.payment_type = res.data.invoice_information.payment_type //付款方式
                        this.need_invoice = res.data.invoice_information.need_invoice //是否需要发票，1需要，0不需要
                        this.invoice_type = res.data.invoice_information.invoice_type //发票类型，1电子发票，2纸质发票
                        // this.invoice_class = res.data.invoice_class //开票类型
                        // this.invoice_name = res.data.invoice_name //发票名称
                        this.invoice_company = res.data.invoice_information.invoice_company //公司名称
                        this.invoice_tax_code = res.data.invoice_information.invoice_tax_code //税号
                        this.invoice_address = res.data.invoice_information.invoice_address //公司地址
                        this.invoice_mobile = res.data.invoice_information.invoice_mobile //公司电话	
                        this.invoice_bank = res.data.invoice_information.invoice_bank //开户行
                        this.invoice_bank_code = res.data.invoice_information.invoice_bank_code //开户账号
                        this.invoice_pay = res.data.invoice_information.invoice_pay //是否见票付款，1是，0否
                    } else {
                        this.$Toast(res.msg);
                    }
                })
                .catch(err => { });

        },
        getInfo1() {
            let params = {
                wx_member_id: localStorage.getItem('wx_member_id'),
                token: localStorage.getItem('token'),
                cate_id: 2,
            };
            this.$post("/api/member_goods/get_product", params)
                .then(res => {
                    if (res.code == 1) {
                        this.goodsList = res.data.goods
                        this.learnNormList = res.data.edu_standard_list
                        this.examRankList = res.data.edu_level_list

                        this.getInfo();
                    } else {
                        this.$Toast(res.msg);
                    }
                })
                .catch(err => { });

        },
        timeTurn(val) {
            return this.$formatDateTime(val, 'yyyy-MM-dd');
        },
        timeConfirm(type) {
            if (type == 1) {
                this.edu_enter_time = this.timeVal1
                this.timePop1 = false
            }
            if (type == 2) {
                this.start_time = this.timeVal2
                this.timePop2 = false
            }
            if (type == 3) {
                this.over_time = this.timeVal3
                this.timePop3 = false
            }
            if (type == 4) {
                this.end_time = this.timeVal4
                this.timePop4 = false
            }
        },
        onGoodsSelect(value, index) {
            console.log(index)
            this.goodsIndex = index;
            this.goodsName = value.name;
            this.goodsPicker = false;
        },
        // 学习标准
        onLearnNorm(value, index) {
            // this.learnNormValue = value;
            this.edu_standard = value;
            this.learnNormPicker = false;
        },
        // 报考级别
        onExamRank(value) {
            // this.examRankValue = value;
            this.edu_level = value;
            this.examRankPicker = false;
        },
        // 保存信息
        pushInfo() {
            console.log(this.goodsIndex)
            console.log(!this.goodsName)
            if (!this.goodsName) {
                this.$Toast('请选择产品');
                return;
            }
            if (!this.price) {
                this.$Toast('请输入学费');
                return;
            }
            if (!this.edu_standard) {
                this.$Toast('请选择学习标准');
                return;
            }

            if (!this.edu_education) {
                this.$Toast('请输入前置学历');
                return;
            }
            let params = {
                wx_member_id: localStorage.getItem('wx_member_id'),
                token: localStorage.getItem('token'),
                member_goods_id: this.basic_information.member_goods_id,
                id: this.basic_information.id,
                name: this.name,
                mobile: this.mobile,
                sex: this.sex,
                birthday: this.$formatDateTime(this.birthday, 'yyyy-MM-dd'),
                id_card: this.id_card,
                address: this.address,

                edu_direction: this.edu_direction,
                goods_id: this.goodsList[this.goodsIndex].id,
                price: this.price,
                edu_enter_time: this.timeTurn(this.edu_enter_time),
                start_time: this.timeTurn(this.start_time),
                over_time: this.timeTurn(this.over_time),
                end_time: this.timeTurn(this.end_time),
                edu_standard: this.edu_standard,
                edu_level: this.edu_level,
                edu_education: this.edu_education,
                edu_degree: this.edu_pass,
                edu_pass: this.edu_pass,



                lag_pay: this.lag_pay, //是否允许后付款
                payment_type: this.payment_type, //付款方式
                need_invoice: this.need_invoice, //是否需要发票，1需要，0不需要
                invoice_type: this.invoice_type,//发票类型，1电子发票，2纸质发票
                // this.invoice_class = res.data.invoice_class //开票类型
                // this.invoice_name = res.data.invoice_name //发票名称

                invoice_class: this.invoiceClassSource[this.invoiceClassIndex].id, //开票类型
                invoice_name: this.invoiceNameSource[this.invoiceNameIndex], //发票名称
                new_invoice_name: this.new_invoice_name, //自定义发票名称

                invoice_company: this.invoice_company,//公司名称
                invoice_tax_code: this.invoice_tax_code, //税号
                invoice_address: this.invoice_address, //公司地址
                invoice_mobile: this.invoice_mobile, //公司电话	
                invoice_bank: this.invoice_bank, //开户行
                invoice_bank_code: this.invoice_bank_code, //开户账号
                invoice_pay: this.invoice_pay, //是否见票付款，1是，0否
            };
            this.$post("/api/Education/save_data", params)
                .then(res => {
                    if (res.code == 1) {
                        this.$Toast(res.msg);
                        setTimeout(() => {
                            this.$router.go(-1)
                        }, 1500);
                    } else {
                        this.$Toast(res.msg);
                    }
                })
                .catch(err => { });
        },

        // 发票类型
        onInvoice(value, index) {
            this.invoiceTypeIndex = index;
            this.invoiceTypeName = value.name
            this.invoicePickerShow = false;
        },
        // 开票类型
        onMakeInvoice(value, index) {
            this.invoiceClassIndex = index;
            this.invoiceClassName = value.name
            this.invoiceClassPickerShow = false;
        },
        // 公司名称
        onInvoiceName(value, index) {
            this.invoiceNameIndex = index;
            this.invoiceNameName = value
            this.invoiceNameShow = false;
        },
        // 业务人员多选回调
        selectResult(type, title, Arr, val3) {
            if (type == 0) {
                this.salesmanName = title
                this.salesmanValArr = Arr
            }
            if (type == 1) {
                this.customerName = title
                this.customerValArr = Arr
            }
            if (type == 2) {
                this.financeName = title
                this.financeValArr = Arr
            }
            if (type == 3) {
                this.compensateName = title
                this.compensateValArr = Arr
            }
        },
    },
    created() {
        // 实例创建完成
        // this.getInfo();
        this.getInfo1();
        this.getConfig();

    },
    beforeCreate() {
        document.querySelector("body").setAttribute("style", "background-color:#fff");
    }
}
</script>

<style scoped lang='less'>
.radio {
    display: flex;
    align-items: center;
    padding: 10px;

    div:nth-of-type(1) {
        width: 126px;
        font-size: 14px;
    }
}
</style>